package severlet;

import bean.Student;
import dao.StudentDao;
import service.StudenService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private StudenService studenService;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username==null || "".equals(username)){
            request.setAttribute("nameEmpty","用户名为空");
            System.out.println("用户名为空");
        request.getRequestDispatcher("student_login.jsp").forward(request,response);
        }
        if (password==null || "".equals(password)){
            request.setAttribute("psdEmpty","密码为空");
            System.out.println("密码名为空");
            request.getRequestDispatcher("student_login.jsp").forward(request,response);
        }


//验证码的判断
        String yanzhengma = request.getParameter("verifyCode");
        String code = (String) request.getSession().getAttribute("uri");
        if ("".equals(yanzhengma)||yanzhengma==null ){
            request.setAttribute("codeEmpty","验证码为空");
            System.out.println("验证码为空");
            request.getRequestDispatcher("student_login.jsp").forward(request,response);
        }

        if (yanzhengma.equalsIgnoreCase(code)) {


            studenService = new StudenService();
            boolean flag = studenService.login(request, response, username, password);

            if (flag == true && yanzhengma.equalsIgnoreCase(code)) {
                System.out.println("登录成功");

                request.getRequestDispatcher("student_information.jsp").forward(request, response);
            }else {
                request.setAttribute("psdErr","密码错误");
                request.getRequestDispatcher("student_login.jsp").forward(request,response);
            }
        }else {
            request.setAttribute("codeErr","验证码错误");
            request.getRequestDispatcher("student_login.jsp").forward(request,response);
        }
    }
}
